/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * HolidayEdit.java
 *
 * Created on 19-Jun-2010, 00:28:18
 */

package TimeRecording.forms;

import TimeRecording.enums.HolidayAdjustmentStatus;
import TimeRecording.tables.HolidayAdjustment;
import TimeRecording.widgets.EmployeeComboBox;
import TimeRecording.widgets.HolidayGrantTypeComboBox;
import java.beans.PropertyChangeListener;
import java.beans.PropertyChangeSupport;
import javax.swing.JOptionPane;
import org.jdesktop.application.Action;

/**
 *
 * @author david
 */
public class HolidayAdjustmentEditor extends javax.swing.JDialog {

    /** Creates new form HolidayEdit */
    public HolidayAdjustmentEditor(HolidayAdjustmentDialog parent) {
        super(parent, ModalityType.APPLICATION_MODAL);
        myParent = parent;
        initComponents();
    }

    // <editor-fold defaultstate="collapsed" desc="Property Change Support">
    private PropertyChangeSupport changeSupport = new PropertyChangeSupport(this);

    @Override
    public void addPropertyChangeListener(PropertyChangeListener listener) {
	changeSupport.addPropertyChangeListener(listener);
    }

    @Override
    public void removePropertyChangeListener(PropertyChangeListener listener) {
	changeSupport.removePropertyChangeListener(listener);
    } // </editor-fold>

    // <editor-fold defaultstate="collapsed" desc="transientAdjustment + Getter + Setter">
    private HolidayAdjustment transientAdjustment = new HolidayAdjustment();

    public HolidayAdjustment getTransientAdjustment() {
        return transientAdjustment;
    }

    public void setTransientAdjustment(HolidayAdjustment transientAdjustment) {
        HolidayAdjustment oldTransientAdjustment = this.transientAdjustment;
        this.transientAdjustment = transientAdjustment;
        changeSupport.firePropertyChange("transientAdjustment", oldTransientAdjustment, transientAdjustment);
    } // </editor-fold>

    // <editor-fold defaultstate="collapsed" desc="persistentAdjustment + Getter + Setter">
    private HolidayAdjustment persistentAdjustment;

    public HolidayAdjustment getPersistentAdjustment() {
        return persistentAdjustment;
    }

    public void setPersistentAdjustment(HolidayAdjustment persistentAdjustment) {
        HolidayAdjustment oldPersistentAdjustment = this.persistentAdjustment;
        this.persistentAdjustment = persistentAdjustment;
        changeSupport.firePropertyChange("persistentAdjustment", oldPersistentAdjustment, persistentAdjustment);
    } // </editor-fold>

    // <editor-fold defaultstate="collapsed" desc="administratorLoggedIn + Getter + Setter">
    private boolean administratorLoggedIn;

    public boolean isAdministratorLoggedIn() {
        return administratorLoggedIn;
    }

    public void setAdministratorLoggedIn(boolean administratorLoggedIn) {
        boolean oldAdministratorLoggedIn = this.administratorLoggedIn;
        this.administratorLoggedIn = administratorLoggedIn;
        changeSupport.firePropertyChange("administratorLoggedIn", oldAdministratorLoggedIn, administratorLoggedIn);
    } // </editor-fold>

    // <editor-fold defaultstate="collapsed" desc="administratorEditable + Getter + Setter">
    private boolean administratorEditable = false;

    public boolean isAdministratorEditable() {
        return administratorEditable;
    }

    public void setAdministratorEditable(boolean administratorEditable) {
        boolean oldAdministratorEditable = this.administratorEditable;
        this.administratorEditable = administratorEditable;
        changeSupport.firePropertyChange("administratorEditable", oldAdministratorEditable, administratorEditable);
    } // </editor-fold>

    // <editor-fold defaultstate="collapsed" desc="saveSelected + Getter + Setter">
    private boolean saveSelected;

    public boolean isSaveSelected() {
        return saveSelected;
    }

    public void setSaveSelected(boolean saveSelected) {
        boolean oldSaveSelected = this.saveSelected;
        this.saveSelected = saveSelected;
        changeSupport.firePropertyChange("saveSelected", oldSaveSelected, saveSelected);
    } // </editor-fold>

    public void setHolidayAdjustment(HolidayAdjustment adjustment) {
        
        setPersistentAdjustment(adjustment);

        transientAdjustment.copy(adjustment);

        setAdministratorEditable(isAdministratorLoggedIn() &&
                adjustment.getStatus() != HolidayAdjustmentStatus.PROCESSED &&
                adjustment.getStatus() != HolidayAdjustmentStatus.ARCHIVED);
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {
        java.awt.GridBagConstraints gridBagConstraints;
        bindingGroup = new org.jdesktop.beansbinding.BindingGroup();

        entityManager = myParent.getEntityManager();
        dateConverter = new TimeRecording.widgets.DateConverter();
        holidayRequestDetails = new javax.swing.JPanel();
        employeeLabel = new javax.swing.JLabel();
        employee = new EmployeeComboBox(entityManager);
        administratorLabel = new javax.swing.JLabel();
        administrator = new javax.swing.JTextField();
        commentLabel = new javax.swing.JLabel();
        comment = new javax.swing.JTextField();
        adjustmentDateLabel = new javax.swing.JLabel();
        fromDate = new com.toedter.calendar.JDateChooser();
        hoursSpentLabel = new javax.swing.JLabel();
        newHoursAdjusted = new javax.swing.JFormattedTextField();
        buttonPanel = new javax.swing.JPanel();
        saveButton = new javax.swing.JButton();
        cancelButton = new javax.swing.JButton();
        typeLabel = new javax.swing.JLabel();
        type = new HolidayGrantTypeComboBox();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
        org.jdesktop.application.ResourceMap resourceMap = org.jdesktop.application.Application.getInstance(TimeRecording.TimeRecordingApp.class).getContext().getResourceMap(HolidayAdjustmentEditor.class);
        setTitle(resourceMap.getString("Form.title")); // NOI18N
        setName("Form"); // NOI18N
        getContentPane().setLayout(new javax.swing.BoxLayout(getContentPane(), javax.swing.BoxLayout.PAGE_AXIS));

        holidayRequestDetails.setBorder(javax.swing.BorderFactory.createEmptyBorder(4, 8, 4, 8));
        holidayRequestDetails.setName("holidayRequestDetails"); // NOI18N
        holidayRequestDetails.setLayout(new java.awt.GridBagLayout());

        employeeLabel.setText(resourceMap.getString("employeeLabel.text")); // NOI18N
        employeeLabel.setName("employeeLabel"); // NOI18N
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 0;
        gridBagConstraints.gridy = 0;
        gridBagConstraints.anchor = java.awt.GridBagConstraints.EAST;
        gridBagConstraints.insets = new java.awt.Insets(8, 4, 4, 4);
        holidayRequestDetails.add(employeeLabel, gridBagConstraints);

        employee.setName("employee"); // NOI18N
        employee.setPreferredSize(new java.awt.Dimension(150, 20));

        org.jdesktop.beansbinding.Binding binding = org.jdesktop.beansbinding.Bindings.createAutoBinding(org.jdesktop.beansbinding.AutoBinding.UpdateStrategy.READ_WRITE, this, org.jdesktop.beansbinding.ELProperty.create("${transientAdjustment.employeeId}"), employee, org.jdesktop.beansbinding.BeanProperty.create("selectedItem"));
        bindingGroup.addBinding(binding);
        binding = org.jdesktop.beansbinding.Bindings.createAutoBinding(org.jdesktop.beansbinding.AutoBinding.UpdateStrategy.READ_WRITE, this, org.jdesktop.beansbinding.ELProperty.create("${administratorEditable}"), employee, org.jdesktop.beansbinding.BeanProperty.create("enabled"));
        bindingGroup.addBinding(binding);

        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 1;
        gridBagConstraints.gridy = 0;
        gridBagConstraints.gridwidth = 2;
        gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL;
        gridBagConstraints.anchor = java.awt.GridBagConstraints.WEST;
        gridBagConstraints.insets = new java.awt.Insets(8, 4, 4, 8);
        holidayRequestDetails.add(employee, gridBagConstraints);

        administratorLabel.setText(resourceMap.getString("administratorLabel.text")); // NOI18N
        administratorLabel.setName("administratorLabel"); // NOI18N
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 3;
        gridBagConstraints.gridy = 0;
        gridBagConstraints.anchor = java.awt.GridBagConstraints.EAST;
        gridBagConstraints.insets = new java.awt.Insets(8, 12, 4, 4);
        holidayRequestDetails.add(administratorLabel, gridBagConstraints);

        administrator.setEditable(false);
        administrator.setName("administrator"); // NOI18N
        administrator.setPreferredSize(new java.awt.Dimension(150, 20));

        binding = org.jdesktop.beansbinding.Bindings.createAutoBinding(org.jdesktop.beansbinding.AutoBinding.UpdateStrategy.READ_WRITE, this, org.jdesktop.beansbinding.ELProperty.create("${transientAdjustment.administratorId.friendlyName}"), administrator, org.jdesktop.beansbinding.BeanProperty.create("text"));
        bindingGroup.addBinding(binding);

        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 4;
        gridBagConstraints.gridy = 0;
        gridBagConstraints.gridwidth = 2;
        gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL;
        gridBagConstraints.anchor = java.awt.GridBagConstraints.WEST;
        gridBagConstraints.insets = new java.awt.Insets(8, 4, 4, 4);
        holidayRequestDetails.add(administrator, gridBagConstraints);

        commentLabel.setText(resourceMap.getString("commentLabel.text")); // NOI18N
        commentLabel.setName("commentLabel"); // NOI18N
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 0;
        gridBagConstraints.gridy = 1;
        gridBagConstraints.anchor = java.awt.GridBagConstraints.EAST;
        gridBagConstraints.insets = new java.awt.Insets(4, 4, 4, 4);
        holidayRequestDetails.add(commentLabel, gridBagConstraints);

        comment.setName("comment"); // NOI18N

        binding = org.jdesktop.beansbinding.Bindings.createAutoBinding(org.jdesktop.beansbinding.AutoBinding.UpdateStrategy.READ_WRITE, this, org.jdesktop.beansbinding.ELProperty.create("${transientAdjustment.comment}"), comment, org.jdesktop.beansbinding.BeanProperty.create("text"));
        bindingGroup.addBinding(binding);
        binding = org.jdesktop.beansbinding.Bindings.createAutoBinding(org.jdesktop.beansbinding.AutoBinding.UpdateStrategy.READ_WRITE, this, org.jdesktop.beansbinding.ELProperty.create("${administratorEditable}"), comment, org.jdesktop.beansbinding.BeanProperty.create("editable"));
        bindingGroup.addBinding(binding);

        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 1;
        gridBagConstraints.gridy = 1;
        gridBagConstraints.gridwidth = 5;
        gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL;
        gridBagConstraints.weightx = 100.0;
        gridBagConstraints.insets = new java.awt.Insets(4, 4, 4, 4);
        holidayRequestDetails.add(comment, gridBagConstraints);

        adjustmentDateLabel.setText(resourceMap.getString("adjustmentDateLabel.text")); // NOI18N
        adjustmentDateLabel.setName("adjustmentDateLabel"); // NOI18N
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 0;
        gridBagConstraints.gridy = 2;
        gridBagConstraints.anchor = java.awt.GridBagConstraints.EAST;
        gridBagConstraints.insets = new java.awt.Insets(4, 4, 4, 4);
        holidayRequestDetails.add(adjustmentDateLabel, gridBagConstraints);

        fromDate.setMinimumSize(new java.awt.Dimension(150, 20));
        fromDate.setName("fromDate"); // NOI18N
        fromDate.setPreferredSize(new java.awt.Dimension(80, 20));

        binding = org.jdesktop.beansbinding.Bindings.createAutoBinding(org.jdesktop.beansbinding.AutoBinding.UpdateStrategy.READ_WRITE, this, org.jdesktop.beansbinding.ELProperty.create("${transientAdjustment.adjustmentDate}"), fromDate, org.jdesktop.beansbinding.BeanProperty.create("date"));
        bindingGroup.addBinding(binding);
        binding = org.jdesktop.beansbinding.Bindings.createAutoBinding(org.jdesktop.beansbinding.AutoBinding.UpdateStrategy.READ_WRITE, this, org.jdesktop.beansbinding.ELProperty.create("${administratorEditable}"), fromDate, org.jdesktop.beansbinding.BeanProperty.create("enabled"));
        bindingGroup.addBinding(binding);

        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 1;
        gridBagConstraints.gridy = 2;
        gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL;
        gridBagConstraints.anchor = java.awt.GridBagConstraints.WEST;
        gridBagConstraints.weightx = 40.0;
        gridBagConstraints.insets = new java.awt.Insets(4, 4, 4, 4);
        holidayRequestDetails.add(fromDate, gridBagConstraints);

        hoursSpentLabel.setText(resourceMap.getString("hoursSpentLabel.text")); // NOI18N
        hoursSpentLabel.setName("hoursSpentLabel"); // NOI18N
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 2;
        gridBagConstraints.gridy = 2;
        gridBagConstraints.anchor = java.awt.GridBagConstraints.EAST;
        gridBagConstraints.insets = new java.awt.Insets(4, 12, 4, 4);
        holidayRequestDetails.add(hoursSpentLabel, gridBagConstraints);

        newHoursAdjusted.setText(resourceMap.getString("newHoursAdjusted.text")); // NOI18N
        newHoursAdjusted.setName("newHoursAdjusted"); // NOI18N
        newHoursAdjusted.setPreferredSize(new java.awt.Dimension(80, 20));

        binding = org.jdesktop.beansbinding.Bindings.createAutoBinding(org.jdesktop.beansbinding.AutoBinding.UpdateStrategy.READ_WRITE, this, org.jdesktop.beansbinding.ELProperty.create("${transientAdjustment.hoursAdjusted}"), newHoursAdjusted, org.jdesktop.beansbinding.BeanProperty.create("value"));
        bindingGroup.addBinding(binding);
        binding = org.jdesktop.beansbinding.Bindings.createAutoBinding(org.jdesktop.beansbinding.AutoBinding.UpdateStrategy.READ_WRITE, this, org.jdesktop.beansbinding.ELProperty.create("${administratorEditable}"), newHoursAdjusted, org.jdesktop.beansbinding.BeanProperty.create("editable"));
        bindingGroup.addBinding(binding);

        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 3;
        gridBagConstraints.gridy = 2;
        gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL;
        gridBagConstraints.anchor = java.awt.GridBagConstraints.WEST;
        gridBagConstraints.weightx = 20.0;
        gridBagConstraints.insets = new java.awt.Insets(4, 4, 4, 4);
        holidayRequestDetails.add(newHoursAdjusted, gridBagConstraints);

        buttonPanel.setName("buttonPanel"); // NOI18N

        javax.swing.ActionMap actionMap = org.jdesktop.application.Application.getInstance(TimeRecording.TimeRecordingApp.class).getContext().getActionMap(HolidayAdjustmentEditor.class, this);
        saveButton.setAction(actionMap.get("saveClicked")); // NOI18N
        saveButton.setText(resourceMap.getString("saveButton.text")); // NOI18N
        saveButton.setName("saveButton"); // NOI18N
        buttonPanel.add(saveButton);

        cancelButton.setAction(actionMap.get("cancelClicked")); // NOI18N
        cancelButton.setText(resourceMap.getString("cancelButton.text")); // NOI18N
        cancelButton.setName("cancelButton"); // NOI18N
        buttonPanel.add(cancelButton);

        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 0;
        gridBagConstraints.gridy = 3;
        gridBagConstraints.gridwidth = 6;
        gridBagConstraints.fill = java.awt.GridBagConstraints.BOTH;
        holidayRequestDetails.add(buttonPanel, gridBagConstraints);

        typeLabel.setText(resourceMap.getString("typeLabel.text")); // NOI18N
        typeLabel.setName("typeLabel"); // NOI18N
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 4;
        gridBagConstraints.gridy = 2;
        gridBagConstraints.anchor = java.awt.GridBagConstraints.EAST;
        gridBagConstraints.insets = new java.awt.Insets(4, 4, 4, 4);
        holidayRequestDetails.add(typeLabel, gridBagConstraints);

        type.setName("type"); // NOI18N
        type.setPreferredSize(new java.awt.Dimension(100, 20));

        binding = org.jdesktop.beansbinding.Bindings.createAutoBinding(org.jdesktop.beansbinding.AutoBinding.UpdateStrategy.READ_WRITE, this, org.jdesktop.beansbinding.ELProperty.create("${transientAdjustment.adjustmentType}"), type, org.jdesktop.beansbinding.BeanProperty.create("selectedItem"));
        bindingGroup.addBinding(binding);

        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 5;
        gridBagConstraints.gridy = 2;
        gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL;
        gridBagConstraints.anchor = java.awt.GridBagConstraints.WEST;
        gridBagConstraints.weightx = 40.0;
        gridBagConstraints.insets = new java.awt.Insets(4, 4, 4, 4);
        holidayRequestDetails.add(type, gridBagConstraints);

        getContentPane().add(holidayRequestDetails);

        bindingGroup.bind();

        java.awt.Dimension screenSize = java.awt.Toolkit.getDefaultToolkit().getScreenSize();
        setBounds((screenSize.width-732)/2, (screenSize.height-191)/2, 732, 191);
    }// </editor-fold>//GEN-END:initComponents

    private boolean validateHolidayAdjustment() {
        
        if (transientAdjustment.getEmployeeId() == null) {

            JOptionPane.showMessageDialog(this,
                    "Insufficient details supplied",
                    "Save Holiday Adjustment",
                    JOptionPane.ERROR_MESSAGE);

            return false;
        }

        return true;
    }

    @Action
    public void cancelClicked() {
        setSaveSelected(false);
        setVisible(false);
    }

    @Action
    public void saveClicked() {

        if (!validateHolidayAdjustment()) {
            return;
        }

        // Transfer transient values to persistent ones

        persistentAdjustment.copy(transientAdjustment);
        
        setSaveSelected(true);
        setVisible(false);
    }

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JLabel adjustmentDateLabel;
    private javax.swing.JTextField administrator;
    private javax.swing.JLabel administratorLabel;
    private javax.swing.JPanel buttonPanel;
    private javax.swing.JButton cancelButton;
    private javax.swing.JTextField comment;
    private javax.swing.JLabel commentLabel;
    private TimeRecording.widgets.DateConverter dateConverter;
    private javax.swing.JComboBox employee;
    private javax.swing.JLabel employeeLabel;
    private javax.persistence.EntityManager entityManager;
    private com.toedter.calendar.JDateChooser fromDate;
    private javax.swing.JPanel holidayRequestDetails;
    private javax.swing.JLabel hoursSpentLabel;
    private javax.swing.JFormattedTextField newHoursAdjusted;
    private javax.swing.JButton saveButton;
    private javax.swing.JComboBox type;
    private javax.swing.JLabel typeLabel;
    private org.jdesktop.beansbinding.BindingGroup bindingGroup;
    // End of variables declaration//GEN-END:variables

    private HolidayAdjustmentDialog myParent;
}